﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using Hutech.Authentication.Library.Data;


namespace Hutech.Authentication.Library.Business
{
    public class UserRoles
    {

        #region Constructors
        public UserRoles()
        {
        }

        public UserRoles(Guid id)
        {
            GetUserRoles(id);
        }

        #endregion

        #region Private Properties

        private Guid iD = Guid.Empty;
        private Guid userId = Guid.Empty;
        private Guid roleId = Guid.Empty;
        private string name = string.Empty;


        #endregion

        #region Public Properties
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        public Guid ID
        {
            get
            {
                return iD;
            }
            set
            {
                iD = value;
            }
        }
        public Guid UserId
        {
            get
            {
                return userId;
            }
            set
            {
                userId = value;
            }
        }
        public Guid RoleId
        {
            get
            {
                return roleId;
            }
            set
            {
                roleId = value;
            }
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Gets an instance of UserRoles.
        /// </summary>
        /// <param name="id"> id </param>
        private void GetUserRoles(Guid id)
        {
            using (IDataReader reader = DBUserRoles.GetOne(id))
            {
                PopulateFromReader(reader);
            }
        }


        private void PopulateFromReader(IDataReader reader)
        {
            if (reader.Read())
            {
                this.iD = new Guid(reader["ID"].ToString());
                this.userId = new Guid(reader["UserId"].ToString());
                this.roleId = new Guid(reader["RoleId"].ToString());
            }
        }

        /// <summary>
        /// Persists a new instance of UserRoles. Returns true on success.
        /// </summary>
        /// <returns></returns>
        private bool Create()
        {
            this.iD = Guid.NewGuid();

            int rowsAffected = DBUserRoles.Create(
            this.iD,
            this.userId,
            this.roleId);

            return (rowsAffected > 0);
        }


        /// <summary>
        /// Updates this instance of UserRoles. Returns true on success.
        /// </summary>
        /// <returns>bool</returns>
        private bool Update()
        {
            return DBUserRoles.Update(this.userId, this.roleId);
        }
        #endregion

        #region Public Methods

        /// <summary>
        /// Saves this instance of UserRoles. Returns true on success.
        /// </summary>
        /// <returns>bool</returns>
        public bool Save()
        {
            //Chua co role
            if (this.iD != Guid.Empty)
            {
                return Update();
            }
            else
            {
                return Create();
            }
        }
        #endregion

        #region Static Methods

        /// <summary>
        /// Deletes an instance of UserRoles. Returns true on success.
        /// </summary>
        /// <param name="id"> id </param>
        /// <returns>bool</returns>
        public static bool Delete(Guid id)
        {
            return DBUserRoles.Delete(id);
        }


        /// <summary>
        /// Gets a count of UserRoles. 
        /// </summary>
        public static int GetCount()
        {
            return DBUserRoles.GetCount();
        }

        private static List<UserRoles> LoadListFromReader(IDataReader reader)
        {
            List<UserRoles> UserRolesList = new List<UserRoles>();
            try
            {
                while (reader.Read())
                {
                    UserRoles UserRoles = new UserRoles();
                    UserRoles.iD = new Guid(reader["ID"].ToString());
                    UserRoles.userId = new Guid(reader["UserId"].ToString());
                    UserRoles.roleId = new Guid(reader["RoleId"].ToString());
                    UserRolesList.Add(UserRoles);
                }
            }
            finally
            {
                reader.Close();
            }

            return UserRolesList;
        }

        /// <summary>
        /// Gets an IList with all instances of UserRoles.
        /// </summary>
        public static List<UserRoles> GetAll()
        {
            return LoadListFromReader(DBUserRoles.GetAll());
        }

        /// <summary>
        /// Gets an IList with page of instances of UserRoles.
        /// </summary>
        /// <param name="pageNumber">The page number.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <param name="totalPages">total pages</param>
        public static List<UserRoles> GetPage(int pageNumber, int pageSize, out int totalPages)
        {
            totalPages = 1;
            return LoadListFromReader(DBUserRoles.GetPage(pageNumber, pageSize, out totalPages));
        }

        public static Guid getUserIDByUser(string userName)
        {
            using (IDataReader reader = DBUserRoles.getUserIDByUser(userName))
            {
                if (reader.Read())
                {
                    return new Guid(reader["UserId"].ToString());
                }
                return Guid.Empty;
            }
        }

        public static Guid getUserRoleIDByUserID(Guid _UserID)
        {
            using (IDataReader reader = DBUserRoles.getUserRoleIDByUserID(_UserID))
            {
                if (reader.Read())
                {
                    return new Guid(reader["ID"].ToString());
                }
                return Guid.Empty;
            }
        }
        #endregion

        #region Add by tsandtm 20/9/2012
        /// <summary>
        /// Get all role of user
        /// </summary>
        /// <param name="UserID"></param>
        /// <returns></returns>
        internal static List<UserRoles> GetAllByUserID(Guid UserID)
        {
            return LoadListFromReader(DBUserRoles.GetAllByUserID(UserID));
        }
        #endregion
    }
}
